(function(){
	var pagenum=10;//每页数量
	var curpage=1;//当前页面
	var totalPage=0;//总页数
	//获取列表
	function getlist(curpage,pagenum){
	   var mallName=$("#mallName").val();
	   var priceType=$("#priceType").val()=="全部"?"":$("#priceType").val();
	   var orderStatus=$("#orderStatus").val()=="全部"?"":$("#orderStatus").val();
	   var orderId=$("#orderId").val();
	   var startDate=$("#datepicker").val();
	   var endDate=$("#datepicker2").val();
	   $.ajax({
			url:'http://web.icecn.net/web/findOrderList.json',
			type:'POST',
			async:true,
			data:{
                offset:pagenum*(curpage-1),
				limit:pagenum,
				mallName:mallName,
				priceType:priceType,
				orderStatus:orderStatus,
				orderId:orderId,
				startDate:startDate,
				endDate:endDate
			},
			xhrFields:{withCredentials:true},
			dataType: "json",
			success: function(data){
				if(data.code == 200 ){
				    dataset=data.t.result;
					if(dataset && dataset.length > 0){
					   var html = template('datalist',{dataset:dataset});
					   $("#example1").html(html);
					   totalPage=data.t.totalPage;
					   $(".pageinfo").html("当前"+curpage+"页/共"+totalPage+"页");
					   $(".curpage").val(curpage);
					   if(curpage==1){$(".prev").addClass("disabled")}else{$(".prev").removeClass("disabled")}
					   if(curpage==totalPage){$(".next").addClass("disabled")}else{$(".next").removeClass("disabled")}
					}else{
					  $("#example1").html("<tbody><tr><td>查不到数据</td></tr></tbody>");
					};
				}
			}
	   });
	}
	var Page = {
		init:function(){
			this.render();
			this.event();
		},
		//绑定事件
		event:function(){
			//上一页
		    $(".prev").click(
			    function(){
					if(curpage>1){
				     	curpage=curpage-1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//下一页
		    $(".next").click(
			    function(){
					if(curpage<totalPage){
				     	curpage=curpage+1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//页码跳转
			$(".gopage").click(
			    function(){
					if($(".curpage").val()>=1&&$(".curpage").val()<=totalPage){
				    	curpage=parseInt($(".curpage").val());
					    getlist(curpage,pagenum)
					}else{
						$(".curpage").val(curpage);
					}
			    }
			)
			//搜索按钮
			$(".btnsearch").click(function(){
				curpage=1
			  	getlist(curpage,pagenum)
			})
			//取消订单
			$(".content-wrapper").on("click", '.order', function() {
				$("#modal-order .modal-body").attr("data-id",$(this).attr("data-id"))
			})
			//确认取消订单
			$(".btnorder").click(function(){
			   var id=$("#modal-order .modal-body").attr("data-id");
			   $.ajax({
					url:'http://web.icecn.net/web/cancelOrder.json',
					type:'POST',
					async:true,
					data:{
						id:id
					},
					dataType: "json",
					xhrFields:{withCredentials:true},
					success: function(data){
						if(data.code === 200 ){
							$("#modal-order").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert("取消失败")	
						}
					}
			   });
		    })
			//发货按钮
			$(".content-wrapper").on("click", '.delivery', function() {
				$("#modal-delivery .modal-body").attr("data-id",$(this).attr("data-id"))
				var type=$(this).attr("data-type");
				if(type=="虚拟产品"){
				  $(".transportCompany").html("兑换网址");
				  $(".courierNum").html("兑换码");
				}else{
				  $(".transportCompany").html("快递公司");
				  $(".courierNum").html("快递单号");
				}
				$("#transportCompany").val("");
				$("#courierNum").val("");
			})
			//确认发货
			$(".btndelivery").click(function(){
			   var id=$("#modal-delivery .modal-body").attr("data-id");
			   var transportCompany=$("#transportCompany").val();
			   var courierNum=$("#courierNum").val();
			   if(transportCompany==""||courierNum==""){alert("填写信息不能为空");return}
			   $.ajax({
					url:'http://web.icecn.net/web/deliverGoods.json',
					type:'POST',
					async:true,
					data:{
						id:id,
						transportCompany:transportCompany,
						courierNum:courierNum
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-delivery").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert("发货失败")	
						}
					}
			   });
		    })
		},
		//渲染页面
		render:function(){
			getlist(curpage,pagenum)
		}
	};
	Page.init();
})();